package at.fhj.itm11.forum.service.stubs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import at.fhj.itm11.forum.data.CommentDAO;
import at.fhj.itm11.forum.model.Comment;

public class CommentDAOStub implements CommentDAO {

	private Set<Comment> repo = new HashSet<Comment>();

	@Override
	public Comment findCommentById(Long id) {
		for (Comment comment : repo) {
			if (comment.getId().equals(id)) {
				return comment;
			}
		}

		return null;
	}

	@Override
	public List<Comment> findCommentsByPostOrderedByDate(long postId) {
		List<Comment> result = new ArrayList<Comment>(repo);

		Collections.sort(result, new Comparator<Comment>() {

			@Override
			public int compare(Comment o1, Comment o2) {
				return o1.getDate().compareTo(o2.getDate());
			}
		});

		return result;
	}

	@Override
	public void register(Comment comment) {
		repo.add(comment);
	}

	public Set<Comment> getRepo() {
		return repo;
	}

	public void setRepo(Set<Comment> repo) {
		this.repo = repo;
	}

}
